package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.model.po.dto.AddCourseDto;
import com.xuecheng.content.model.po.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.po.dto.QueryCourseParamsDto;
import com.xuecheng.content.service.CourseBaseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author 张栈(52194440 @ qq.com)
 * @version 1.0
 * @date 2022/10/8 15:31
 */
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {

    //注入课程基础mapper
    @Autowired
    CourseBaseMapper courseBaseMapper;

    @Autowired
    CourseMarketMapper courseMarketMapper;

    @Autowired
    CourseCategoryMapper courseCategoryMapper;


    //分页展示课程信息
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDto queryCourseParamsDto) {
        //分页查询  模糊条件查询 条件查询
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        // 判断查询name
        queryWrapper.like(StringUtils.isNotBlank(queryCourseParamsDto.getCourseName()), CourseBase::getName,
                queryCourseParamsDto.getCourseName());

        // 判断审核状态
        queryWrapper.eq(StringUtils.isNotBlank(queryCourseParamsDto.getAuditStatus()), CourseBase::getAuditStatus,
                queryCourseParamsDto.getAuditStatus());

        // 判断课程发布状态
        queryWrapper.eq(StringUtils.isNotBlank(queryCourseParamsDto.getPublishStatus()), CourseBase::getStatus,
                queryCourseParamsDto.getPublishStatus());

        //排序
        queryWrapper.orderByDesc(CourseBase::getCreateDate);

        //分页
        Page<CourseBase> basePage = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());

        //从数据库拿出数据
        Page<CourseBase> courseBasePage = courseBaseMapper.selectPage(basePage, queryWrapper);

        //取出所有数据
        List<CourseBase> items = courseBasePage.getRecords();

        //统计总数
        long total = courseBasePage.getTotal();

        PageResult<CourseBase> result = new PageResult<CourseBase>(items, total, pageParams.getPageNo(), pageParams.getPageSize());

        return result;
    }

    //添加课程
    @Override
    public CourseBaseInfoDto createCourseBase(Long companyId, AddCourseDto dto) {

        //健壮性判断
        if (StringUtils.isBlank(dto.getName())) {
            throw new RuntimeException("课程姓名为输入");
        }

        if (StringUtils.isBlank(dto.getMt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(dto.getSt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(dto.getGrade())) {
            throw new RuntimeException("课程等级为空");
        }

        if (StringUtils.isBlank(dto.getTeachmode())) {
            throw new RuntimeException("教育模式为空");
        }

        if (StringUtils.isBlank(dto.getUsers())) {
            throw new RuntimeException("适应人群为空");
        }

        if (StringUtils.isBlank(dto.getCharge())) {
            throw new RuntimeException("收费规则为空");
        }
        //添加课程信息
        //创建课程对象封装课程信息
        CourseBase courseBase = new CourseBase();
        //拷贝数据
        BeanUtils.copyProperties(dto, courseBase);
        //设置一些额外数据
        //设置创建时间
        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setAuditStatus("202001");
        courseBase.setStatus("203001");
        //机构id
        courseBase.setCompanyId(companyId);




        //封装课程信息
        int insert = courseBaseMapper.insert(courseBase);
        Long courseId = courseBase.getId();
        //封装明细信息

        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(dto, courseMarket);
        courseMarket.setId(courseId);

        //收费规则判断（收费还是不收费）
        String charge = dto.getCharge();

        if ("201001".equals(charge)) {
            if (Objects.isNull(dto.getPrice())) {
                throw new RuntimeException("课程价格不能为空");
            }
            //如果收费 则封装金额
            courseMarket.setPrice(dto.getPrice().floatValue());
            courseMarket.setOriginalPrice(dto.getOriginalPrice().floatValue());
        }

        //封装到课程营销信息
        int insert1 = courseMarketMapper.insert(courseMarket);


        //判断课程是否添加成功
        if (insert < 0 && insert1 < 0) {
            throw new RuntimeException("课程信息添加失败");
        }


        return getCourseBaseInfo(courseId);
    }

    //课程、课程营销数据封装类
    public CourseBaseInfoDto getCourseBaseInfo(Long courseId){
        //查询出课程和课程营销数据
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);

        //健壮性判断
        if(courseBase == null){
            throw new RuntimeException("课程数据为空");
        }
        if(courseMarket == null){
            throw new RuntimeException("课程营销数据为空");
        }

        //创建dto接收数据
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);


        //封装分类信息
        CourseCategory mt = courseCategoryMapper.selectById(courseBase.getMt());
        CourseCategory st = courseCategoryMapper.selectById(courseBase.getSt());
        courseBaseInfoDto.setMtName(mt.getName());
        courseBaseInfoDto.setStName(st.getName());

        return courseBaseInfoDto;
    }
}
